# coding=utf-8

import datetime
from framework.logger import Logger
from selenium.webdriver.common.keys import Keys
from framework.base_page import BasePage


# create a logger instance
logger = Logger(logger="QuotaListPage").getlog()

class QuotaListPage(BasePage):

    # labels 定位
    labels = 'xpath=>//div[@class="ant-radio-group ant-radio-group-outline ant-radio-group-large"]/label[@class="ant-radio-button-wrapper"]/span[2]'

    # 当前 label的定位
    label = 'xpath=>//label[@class="ant-radio-button-wrapper ant-radio-button-wrapper-checked"]/span[2]'

    # 船舶输入框定位
    div_ship = 'xpath=>//*[@id="ship"]/div/div/div[1]'  # '//div[@id="ship"]/div/div[1]/div[1]'

    # 船舶文本框定位
    input_ship = 'xpath=>//input[@id="ship"]'   # select_ship = 'xpath=>//li[@text="0000001 - 1"]'

    # 船舶列表定位
    lis_ship = 'xpath=>//ul[@class="ant-select-dropdown-menu  ant-select-dropdown-menu-root ant-select-dropdown-menu-vertical"]/li'

    # 展开前的查询按钮
    btn_search_before = "xpath=>//div[@class='ant-col-13']/div/div[1]/button[2]"

    # 高级搜索按钮
    btn_advanced_search = "xpath=>//div[@class='ant-col-13']/div/div[1]/a"

    # 展开后的查询按钮
    btn_search_after = "xpath=>//div[@class='ant-col-24']/div/div[1]/button[2]"

    # 重置按钮
    btn_reset = "xpath=>//div[@class='ant-col-24']/div/div[1]/button[1]"

    # 收起面板按钮
    btn_hide = 'xpath=>//div[@class="ant-col-24"]/div/div[1]/a'

    # eta 输入框
    span_eta = 'xpath=>//*[@id="ETA"]/span' # '//span[@id="ETA"]/span/../..'
    input_eta_start = 'xpath=>/html/body/div[2]/div/div/div/div/div[1]/div[1]/div[1]/div/input'
    input_eta_end = 'xpath=>/html/body/div[2]/div/div/div/div/div[1]/div[2]/div[1]/div/input'
    td_date_1 = 'xpath=>//div[@aria-selected="true"]/..'    # '//div[@aria-selected="true"]' 定位的是被选中的框


    # 服务类型
    div_service_type = 'xpath=>//*[@id="type"]/div/div/div[1]'
    lis_service_type = 'xpath=>/html/body/div[2]/div/div/div/ul/li'

    # 询价方
    div_inquirer = 'xpath=>//*[@id="consigner"]/div/div/div[1]'
    lis_inquirer = 'xpath=>/html/body/div[3]/div/div/div/ul/li'
    input_inquirer = 'xpath=>//*[@id="consigner"]/div/div/div[3]/div/input'

    # 询价单号
    div_inquiry_no = 'xpath=>//*[@id="orderNumber"]/div/div/div[2]'
    input_inquiry_no = 'xpath=>//*[@id="orderNumber"]/div/div/div[2]/div/input'
    lis_inquiry_no = 'xpath=>/html/body/div[2]/div/div/div/ul/li'

    # 排序规则
    div_sortby = 'xpath=>//*[@id="root"]/div/div/div/div[2]/div[2]/div/div[2]/div/form/div[1]/div/div[2]/div/div[2]/div/div/div/span/div'
    lis_sortby = 'xpath=>/html/body/div[2]/div/div/div/ul/li'

    # 查询结果列表
    lis_res = 'xpath=>//*[@id="root"]/div/div/div/div[2]/div[2]/div/div[3]/div/div[1]/div'  # //*[@id="root"]/div/div/div/div[2]/div[2]/div/div[3]/div/div[1]

    # 分页中的上一页按钮
    forward = 'xpath=>//a[@class="ant-pagination-item-link"]/i[@class="anticon anticon-left"]/..'

    # 分页
    pageination = 'xpath=>//ul[@class="ant-pagination mini"]/li'

    # 当前页码
    current_page_number = 'xpath=>'

    # X条/页的选项列表
    list_page_number = 'xpath=>//ul[@class="ant-select-dropdown-menu  ant-select-dropdown-menu-root ant-select-dropdown-menu-vertical"]/li'

    # 被选中的选项-X条/页
    selected_pageination = 'xpath=>//*[@id="root"]/div/div/div/div[2]/div[2]/div/div[3]/div/div[2]/ul/li[10]/div[1]/div/div/div'

    # 输入框-跳至X页
    input_page_number = 'xpath=>//ul[@class="ant-pagination mini"]/li[12]/div[2]/input'

    # 排序规则
    name_sort_by = 'xpath=>//*[@id="orderBy"]/div/div/div[2]'

    # 查询结果具体信息

    # Local Agency - Discharging

    # 第一条报价单的船舶名称'//*[@id="root"]/div/div/div/div[2]/div[2]/div/div[3]/div/div[1]/div[1]/div[1]/div[1]/div[1]'

    # 点击查询按钮-展开前
    def click_search_before(self):
        self.click(self.btn_search_before)

    # 点击查询按钮-展开后
    def click_search_after(self):
        self.click(self.btn_search_after)

    # 点击展开按钮
    def click_advance_search(self):
        self.click(self.btn_advanced_search)

    # 点击收起按钮
    def click_hide(self):
        self.click(self.btn_hide)

    # 下拉框-船名/IMO
    def select_ship(self, name, text=''):
        self.click(self.div_ship)
        if (text is not None):
            if (self.input_ship is not None):
                self.type(self.input_ship, text)
                self.sleep(5)
            else:
                raise NameError("Please enter a element of input_ship.")
        lis = self.find_elements(self.lis_ship)
        for li in lis:
            if name in li.text:
                li.click()
                break
        self.sleep(2)


    # 日期控件-ETA
    def select_eta(self, date_start, date_end):
        try:
            datetime.datetime.strptime(date_start, '%Y-%m-%d')
            datetime.datetime.strptime(date_end, '%Y-%m-%d')
        except ValueError as e:
            logger.error("ValueException: %s" % e)
            # raise ValueError("Incorrect data format, should be YYYY_MM_DD")
        self.click(self.span_eta)
        self.type(self.input_eta_start, date_start)
        self.click(self.td_date_1)
        self.click(self.span_eta)
        self.simulateCtrlA(self.input_eta_end)
        self.type(self.input_eta_end, date_end)
        self.type(self.input_eta_end, Keys.ENTER)  # 模拟回车键操作

    # 下拉框-服务类型
    def select_service_type(self, name_service_type):
        self.click(self.div_service_type)
        lis = self.find_elements(self.lis_service_type)
        for li in lis:
            if name_service_type in li.text:
                li.click()
                break
        self.sleep(2)

    # 下拉框-询价单号
    def select_inquirer(self, name_inquirer):
        self.click(self.div_inquirer)
        self.type(self.input_inquirer)
        lis = self.find_elements(self.lis_inquirer)
        for li in lis:
            if name_inquirer in li.text:
                li.click()
                break
        self.sleep(2)

    # 下拉框-询价单号
    def select_inquiry_no(self, num_inquirer_no):
        self.click(self.div_inquiry_no)
        self.type(self.input_inquiry_no)
        lis = self.find_elements(self.lis_inquiry_no)
        for li in lis:
            if num_inquirer_no in li.text:
                li.click()
                break
        self.sleep(2)

    # 列表中含有card的个数
    # def get_res_list(self):
    #     try:
    #         lis = self.find_elements(self.lis_res)
    #         return len(lis)
    #     except Exception as e:
    #         logger.error(" return is failed with %s" % e)

    # 选择排序规则
    def select_sort_by(self, name_sort):
        self.click(self.div_sortby)
        lis = self.find_elements(self.lis_sortby)
        for li in lis:
            if name_sort in li.text:
                li.click()
                break
        self.sleep(2)


    # 获取报价信息
    def get_quota_info(self, number):

        card_mes = {}
        card_list = 'xpath=>//div[@class="card-list"]/div'
        length = len(self.find_elements(card_list))
        try:
            if(number < length):
                # 船名，IMO和港口
                card_ship_name = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-left"]/div[@class="qc-ship"]/div[@class="qc-ship-name"]'
                card_ship_imo = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-left"]/div[@class="qc-ship"]/div[@class="qc-ship-imo"]'
                card_qc_port = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-left"]/div[@class="qc-port qc-item"]/div'

                # ETA，服务类型和委托方名
                card_qc_time = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-center"]/div[@class="qc-time qc-item"]'
                card_qc_service_type = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-center"]/div[@class="qc-servicetype qc-item"]'
                card_qc_principal = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-center"]/div[@class="qc-principal qc-item"]'

                # 红点，状态，价格和询价单号
                card_new_bool = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-right"]/div[@class="qc-status-box"]/span/sup'
                # card_new_bool = 'xpath=>//div[@class="card-list"]/div[1]/div[@class="quotation-card-item quotation-card-right"]/div[@class="qc-status-box"]/span/sup'
                card_qc_status = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-right"]/div[@class="qc-status-box"]/div[@class="qc-status"]'
                card_qc_price = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-right"]/div[@class="qc-price"]/span/span'
                card_qc_no = card_list + '[' + str(number) + ']/div[@class="quotation-card-item quotation-card-right"]/div[@class="qc-number qc-item"]/span'

                # new 图标
                card_new_icon_bool = card_list + '[' + str(number) + ']/div[@class="new-icon"]/span'

                card_mes['card_ship_name'] = self.find_element(card_ship_name).get_attribute("title")
                card_mes['card_ship_imo'] = self.find_element(card_ship_imo).get_attribute("textContent").split(':')[1]
                card_mes['card_qc_port'] = self.find_element(card_qc_port).get_attribute("textContent")
                card_mes['card_qc_time'] = self.find_element(card_qc_time).get_attribute("textContent")
                card_mes['card_qc_service_type'] = self.find_element(card_qc_service_type).get_attribute("textContent")
                card_mes['card_qc_principal'] = self.find_element(card_qc_principal).get_attribute("textContent")

                card_mes['card_new_bool'] = self.isElementExist(card_new_bool)

                card_mes['card_qc_status'] = self.find_element(card_qc_status).get_attribute("textContent")
                card_mes['card_qc_price'] = self.find_element(card_qc_price).get_attribute("innerText")
                card_mes['card_qc_no'] = self.find_element(card_qc_no).get_attribute("innerText").split(':')[1]
                card_mes['card_new_icon_bool'] = self.isElementExist(card_new_icon_bool)
                return card_mes
            else:
                raise ValueError("你输入的行数大于数据卡行总数！！")
        except Exception as e:
            logger.error("Failed to get_quota_info with: %s" % e)

    # 获取报价列表
    def get_quota_list(self):
        card_list = []

        card_list = 'xpath=>//div[@class="card-list"]/div'
        length = len(self.find_elements(card_list))
        for index in range(length):
            cardinfo = { }
            cardinfo = self.get_quota_info(index)
            card_list.append(cardinfo)
        return card_list

    def select_Label(self, label_name):

        label_list = self.find_elements(self.labels)
        try:
            print("label_name is: " + label_name)
            for label in label_list:
                print("label is: " + label.text)
                if label_name in label.text:
                    label.click()
                    print("%s Label is opened!" % label_name)
                else:
                    raise ValueError("没有找到你要的label")
        except Exception as e:
            logger.error("Failed to the select_Label with %s" % e)

    # 获取当前tab页的名字
    def get_selected_label_name(self):
        label = self.find_elements(self.label)
        return label.text

    # 分页按钮,btn_name = [前一页，后一页，前五页，后五页，1,2,3,4,5，……]
    def click_pagination(self, btn_name):

        try:
            btns = self.find_elements(self.pageination)
            for btn in btns:
                if btn_name in btn.get_attribute("title"):
                    btn.click()
                else:
                    raise ValueError("没有找到对应的按钮")
        except Exception as e:
            logger.error("Failed to the click_pagination with: %s " % e)

    # 设置每页的条数
    def select_numberofpage(self, number):
        self.click(self.selected_pageination)
        els = self.find_elements(self.list_page_number)

        for el in els:
            if str(number) in el.text:
                el.click()
    def get_page_number(self):
        el = self.find_element(self.selected_pageination)
        return el.text

    # 跳转至X页
    def select_page_number(self, page_number):
        self.type(self.input_page_number, page_number)
        self.type(self.input_eta_end, Keys.ENTER)  # 模拟回车键操作

    # 当前页码
    def get_page_number(self):

        try:
            els = self.find_elements(self.pageination)
            for el in els:
                if "ant-pagination-item-active" in el.get_attribute("class"):
                    return el.get_attribute("title")
                else:
                    raise ValueError("没有找到对应的按钮")
        except Exception as e:
            logger.error("Failed to the get_page_number with: %s " % e)

    # 当前排序规则
    def get_sort_by(self):
        names = self.find_elements(self.name_sort_by)
        return names[1].get_attribute("title")

    # 打开详情
    def click_quota_info(self, number):
        card_list = 'xpath=>//div[@class="card-list"]/div'
        length = len(self.find_elements(card_list))

        try:
            if (number < length):
                self.click(card_list + '[' + str(number) + ']')
            else:
                raise ValueError("没有找到对应的数据")
        except Exception as e:
            logger.error("Failed to the click_quota_info with: %s " % e)


        # 船名，IMO和港口

    # def click_ship(self):
    #     self.click(self.select_ship)
    #
    # def click_shiplist(self):
    #     self.click(self.btn_shiplist)
    #
    # def click_eta(self):
    #     self.click(self.btn_eta)